// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {

	async accountlistAction(){
		/*let page = this.post();
		let pages = page.pages;
		let limits = page.limits;
		let search = page.search;
		let zt = page.zt;*/
		let page = JSON.parse(this.post('page'));
		let shopid = this.shop_userinfo.departmentid;

		let where ={
			departmentid:shopid, 
			isdisable:page.zt,
			'name|accountsn':['like','%'+page.search+'%']
		}
		if (page.times && (page.times[0] != null && page.times[1] != null)) {

			let begintime = Math.floor(Date.parse(new Date(page.times[0]))/1000)

			let endtime = Math.floor(Date.parse(new Date(page.times[1]))/1000) + 86400 // 当天加上一天

			where['addtime'] = {'>=': begintime, '<': endtime}

		}  

		let data = await this.model('or_shop_account').where(where).page(page.pages, page.limits).order('id DESC').countSelect();
		return this.json(data)
	}

	async addeditAction(){
		let data = this.post();
		let shopid = this.shop_userinfo.departmentid;
		if (data.id*1) {

			// 判断当前帐号编号是否重复
			let accRepeat = await this.model('or_shop_account').where({departmentid: shopid, number: data.number, id: ['!=', data.id]}).find();

			if (!think.isEmpty(accRepeat)) {
				return this.fail('账户编号重复');
			}

			let chongfu = await this.model('or_shop_account').where({departmentid: shopid, name: data.name, accountsn: data.accsn, id:['!=', data.id]}).select();
			if (!think.isEmpty(chongfu)) {
				return this.fail('更改的账户名称或卡号重复');
			}
			let gengxin = {
				id: data.id,
				name: data.name,
				accounttype: data.type*1,
				accountsn: data.accsn,
				remarks: data.remarks,
				number: data.number
			}
			let res = await this.model('or_shop_account').update(gengxin);
			return res?this.success('更改账户成功'):this.fail('更改账户失败');
		}

		let adaccRepeat = await this.model('or_shop_account').where({departmentid: shopid, number: data.number}).find();
			
		if (!think.isEmpty(adaccRepeat)) {
			return this.fail('账户编号重复');
		}

		let isrepeat = await this.model('or_shop_account').where('(name="'+data.name+'" or accountsn="'+data.accsn+'") and departmentid="'+shopid+'"').select();
		if (!think.isEmpty(isrepeat)) {
			return this.fail('新增账户名称或卡号重复');
		}
		let addtime = Math.round(new Date().valueOf()/1000);

		let addacco = {
			name: data.name,
			departmentid: shopid,
			accounttype: data.type*1,
			accountsn: data.accsn,
			remarks: data.remarks,
			balance: data.money,
			addtime: addtime,
			number: data.number
		}

		let res = await this.model('or_shop_account').add(addacco);
		return res?this.success('添加成功'):this.fail('添加失败');
	}

	async unstopAction(){
		let id = this.post('id');
		let status = this.post('status');
		let res = await this.model('or_shop_account').where({id:id}).update({isdisable: status});
		if (status*1) {
			return res?this.success('弃用成功'):this.fail('弃用失败');
		} else {
			return res?this.success('启用成功'):this.fail('启用失败');
		}
	}

	// 获取账户&收支信息
	async accountlogsAction(){
		let id = this.post('id');
		let pages = this.post('pages');
		let limits = this.post('limits');


		let ainfo = await this.model('or_shop_account').where({id: id}).find();
		let alogs = await this.model('or_shop_accountlog').where({accountid:id}).page(pages, limits).order('id DESC').countSelect();
		



		let alll = await this.model('or_shop_cost').where({accountid:id}).page(pages, limits).order('id DESC').countSelect();
		var aaa = alll.data;
		var bbb = alogs.data
		console.log(aaa)
		console.log(bbb)
		for (var i = 0; i < aaa.length; i++) {
			for (var j = 0; j < bbb.length; j++) {
				if(aaa[i].id === bbb[j].orderid){
					console.log(111111)
					let alls = await this.model('or_shop_costtype').where({id:aaa[i].costtypeid}).find();
					alogs.data[i].typename=alls.typename;
				}
			}
		}
		let data ={
			ainfo: ainfo,
			logs: alogs,
		}
		return this.json(data);
	}
}